#ifndef SERVERREQUESTARRAY_H_
#define SERVERREQUESTARRAY_H_

#include <e32cmn.h>

#include "scatu/ServerRequests.h"

namespace scatu {

class RServerRequestArray : private RPointerArray<RServerRequestBase>
{
public:
    void Close()
        {
        for(TInt i = 0; i < Count(); ++i)
            {
            (*this)[i]->Close();
            }
        RPointerArray::Reset();
        }
    
    template<typename RequestType, typename ServerType, typename FuncPtrType>
    void RegisterRequestL(RequestType & aRequest, ServerType & aServer, FuncPtrType aServeFunction)
        {
        InsertInOrderL(&aRequest, RServerRequestBase::LinearOrder());
        aRequest.SetServeFunctionL(aServer, aServeFunction);
        }
    
    TBool ServeMessageL(const RMessage2& aMessage)
        {
        TInt func = aMessage.Function();
        TInt id = (func < KReturnFunctionBase) ? func : (func - KReturnFunctionBase);  
        
        TInt i = FindInOrder(id, &RServerRequestBase::CompareById);
        if(i < 0) { return EFalse; }
        
        (*this)[i]->ServiceL(aMessage);
        return ETrue;
        }
};

} // namespace scatu

#endif /* SERVERREQUESTARRAY_H_ */
